package org.dromara.cms.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.cms.domain.vo.CmsModelVo;
import org.dromara.cms.domain.bo.CmsModelBo;
import org.dromara.cms.service.ICmsModelService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 内容模型
 *
 * @author Spring
 * @date 2024-12-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cms/model")
public class CmsModelController extends BaseController {

    private final ICmsModelService cmsModelService;

    /**
     * 查询内容模型列表
     */
    @SaCheckPermission("cms:model:list")
    @GetMapping("/list")
    public TableDataInfo<CmsModelVo> list(CmsModelBo bo, PageQuery pageQuery) {
        return cmsModelService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出内容模型列表
     */
    @SaCheckPermission("cms:model:export")
    @Log(title = "内容模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CmsModelBo bo, HttpServletResponse response) {
        List<CmsModelVo> list = cmsModelService.queryList(bo);
        ExcelUtil.exportExcel(list, "内容模型", CmsModelVo.class, response);
    }

    /**
     * 获取内容模型详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cms:model:query")
    @GetMapping("/{id}")
    public R<CmsModelVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(cmsModelService.queryById(id));
    }

    /**
     * 新增内容模型
     */
    @SaCheckPermission("cms:model:add")
    @Log(title = "内容模型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CmsModelBo bo) {
        return toAjax(cmsModelService.insertByBo(bo));
    }

    /**
     * 修改内容模型
     */
    @SaCheckPermission("cms:model:edit")
    @Log(title = "内容模型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CmsModelBo bo) {
        return toAjax(cmsModelService.updateByBo(bo));
    }

    /**
     * 删除内容模型
     *
     * @param ids 主键串
     */
    @SaCheckPermission("cms:model:remove")
    @Log(title = "内容模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(cmsModelService.deleteWithValidByIds(List.of(ids), true));
    }
}
